//https://gitee.com/Ghobam/jsutils/raw/master/MakeMarkdown.js

const COMMON_MD = '---\n{{METADATA}}\n---'

function makeMD(source) {
    let content = '';
    source.forEach((s) => content += getMDFuncByType(s['type'], s['key'], s['value']))
    return getMDResult(content);
}


function getMDFuncByType(type, key, value) {
    switch(type) {
        case 'string':
            return getMDStringNode(key, value);
        case 'number':
            return getMDNumberNode(key, value);
        case 'tag':
            return getMDArrayNode(key, value);
        case 'img':
            return getMDImgNode(key, value);
        case 'relate':
            return getMDRelateNode(key, value);
        default:
            return '';
    }
}

function getMDStringNode(key, value) {
    return `${key}: "${value}"\n`;
}
  
function getMDNumberNode(key, value) {
    return `${key}: ${value}\n`;
}

function getMDArrayNode(key, value) {
    let content = `${key}:\n`;
    value.split(',').forEach((v) => {
        content += `- ${v.trim()}\n`
    })
    return content;
}

function getMDImgNode(key, value) {
    return `${key}: "![](${value})"\n`;
}

function getMDRelateNode(key, value) {
    return `${key}: "[[${value}]]"\n`;
}

function getMDResult(content) {
    let text = COMMON_MD;
    return text.replace('{{METADATA}}', content);
}